package mo.tags.dataflow;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

class StockSpanner {

    private int idx = 0;
    private Stack<Integer> stack = new Stack<>();
    private List<Integer> list = new ArrayList<>();

    public StockSpanner() {
        stack.push(-1);
        list.add(Integer.MAX_VALUE);
    }
    
    public int next(int price) {
        int res = 1;
        list.add(price);
        if (stack.isEmpty()) {
            stack.push(idx);
        } else {
            while (!stack.isEmpty() && list.get(stack.peek()+1) <= price) {
                stack.pop();
            }
            res = idx - stack.peek();
            stack.push(idx);


        }
        idx++;
        return res;
    }

    //["StockSpanner","next","next","next","next","next"]
    //[[],[31],[41],[48],[59],[79]]
    public static void main(String[] args) {
        StockSpanner stockSpanner = new StockSpanner();
        System.out.println(stockSpanner.next(31));
        System.out.println(stockSpanner.next(41));
        System.out.println(stockSpanner.next(48));
        System.out.println(stockSpanner.next(59));
        System.out.println(stockSpanner.next(79));
    }
}